package communication;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class NewJFrame extends javax.swing.JFrame {
	private JTextField jTextField1;
	private JButton start;
	private JTextField jTextField3;
	private JTextField jTextField2;
	Variable v = new Variable();
	Variable x = new Variable();
	Variable d = new Variable();
	Variable z = new Variable();
	ArrayList variaveis;
	MainCOMM m;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewJFrame inst = new NewJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public NewJFrame() {
		super();
		initGUI();
		char c = 0x00;
		char f = 0x01;

		v.setAddress(c);
		v.setControllerAddress(f);
		c = 0x01;

		x.setAddress(c);
		x.setControllerAddress(f);

		c = 0x02;
		d.setAddress(c);
		d.setControllerAddress(f);

		c = 0x03;
		z.setAddress(c);
		z.setControllerAddress(f);

		variaveis = new ArrayList();
		variaveis.add(v);
		variaveis.add(x);
		variaveis.add(d);
		variaveis.add(z);
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setText("LED1");
				jTextField1.setBounds(12, 12, 59, 21);
			}
			{
				jTextField2 = new JTextField();
				getContentPane().add(jTextField2);
				jTextField2.setText("LED2");
				jTextField2.setBounds(158, 12, 59, 21);
			}
			{
				jTextField3 = new JTextField();
				getContentPane().add(jTextField3);
				jTextField3.setText("LED3");
				jTextField3.setBounds(322, 12, 59, 21);
			}
			{
				start = new JButton();
				getContentPane().add(start);
				start.setText("Start");
				start.setBounds(264, 68, 117, 21);
				start.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						startEvent();
					}
				});
			}
			pack();
			this.setSize(400, 134);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void startEvent() {
		m = new MainCOMM();
		m.initiateCommunication(variaveis);

		Thread t = new Thread(new Runnable() {
			public void run() {
				while (true) {

					boolean k = m.isBusyFlag();
					while (k) {
					}

					m.setFree(false);
					int k1 = (int) v.getValue();
					int k2 = (int) x.getValue();
					int k3 = (int) d.getValue();
					m.setFree(true);
					if (k1 == 1)
						jTextField1.setBackground(new Color(255, 128, 0));
					else
						jTextField1.setBackground(new Color(0, 0, 0));

					if (k2 == 1)
						jTextField2.setBackground(new Color(255, 128, 0));
					else
						jTextField2.setBackground(new Color(0, 0, 0));

					if (k3 == 1)
						jTextField3.setBackground(new Color(255, 128, 0));
					else
						jTextField3.setBackground(new Color(0, 0, 0));

					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		// disparar a Thread
		t.start();

	}

}
